package net.huashitong.Project.service;

import net.huashitong.Project.entity.EarlyWarningNode;
import net.huashitong.node.entity.NodeNewschedules;
import net.huashitong.node.service.NodeNewtempletService;
import net.huashitong.oversee.entity.GradePoint;
import net.huashitong.oversee.entity.SupervisionTask;
import net.huashitong.oversee.filter.SupervisionTaskFilter;
import net.huashitong.oversee.service.GradePointService;
import net.huashitong.oversee.service.SupervisionTaskService;
import net.huashitong.oversee.service.TaskRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProjectUserChangeService {
    @Autowired
    @Lazy
    private SupervisionTaskService supervisionTaskService;
    @Autowired
    @Lazy
    private GradePointService gradePointService;
    @Autowired
    @Lazy
    private TaskRecordService recordService;
    @Autowired
    @Lazy
    private NodeNewtempletService nodeNewtempletService;
    @Autowired
    @Lazy
    private EarlyWarningNodeService earlyWarningNodeService;

    //项目经办人修改
    public void updateChangeOperatorUser(String projectId,
                                         String operatorUser,
                                         String operatorPhone,
                                         String userId
    ) {

        SupervisionTaskFilter supervisionTaskFilter = new SupervisionTaskFilter();
        supervisionTaskFilter.setProjectId(projectId);
        List<SupervisionTask> tasks = supervisionTaskService.getTaskListsNoPage(supervisionTaskFilter);

        List<String> approvalNodeIds = nodeNewtempletService.getAllApprovalNodeId();
        for (SupervisionTask t : tasks) {
            if (!approvalNodeIds.contains(t.getNodeId())) {
                //发送短信的预警表
                List<EarlyWarningNode> earlyWarningNodes = earlyWarningNodeService.getEarlyWarningByProjectIdAndNodeId(t.getProjectId(), t.getNodeId());
                for (EarlyWarningNode earlyWarningNode : earlyWarningNodes) {
                    earlyWarningNode.setOperatorUser(operatorUser);
                    earlyWarningNode.setOperatorPhone(operatorPhone);
                }
                earlyWarningNodeService.batchUpdate(earlyWarningNodes);
                //督查表
                t.setOperatorUser(operatorUser);
                t.setOperatorPhone(operatorPhone);
                t.setUserid(userId);
                //绩效表
                List<GradePoint> gradePoints = gradePointService.getGradeById(t.getSysId());
                for (GradePoint g : gradePoints) {
                    if ("业主单位经办人".equals(g.getCheckType())) {
                        g.setCheckObject(operatorUser);
                        g.setCheckObjectId(userId);
                    }
                }
                gradePointService.batchUpdate(gradePoints);
            }
        }
        supervisionTaskService.batchUpdate(tasks);
    }

    //项目责任人修改
    public void updateChangeResUser(String projectId,
                                    String resUser,
                                    String resPhone,
                                    String userId) {
        SupervisionTaskFilter supervisionTaskFilter = new SupervisionTaskFilter();
        supervisionTaskFilter.setProjectId(projectId);
        List<SupervisionTask> tasks = supervisionTaskService.getTaskListsNoPage(supervisionTaskFilter);

        List<String> approvalNodeIds = nodeNewtempletService.getAllApprovalNodeId();
        for (SupervisionTask t : tasks) {
            if (!approvalNodeIds.contains(t.getNodeId())) {
                //发送短信的预警表
                List<EarlyWarningNode> earlyWarningNodes = earlyWarningNodeService.getEarlyWarningByProjectIdAndNodeId(t.getProjectId(), t.getNodeId());
                for (EarlyWarningNode earlyWarningNode : earlyWarningNodes) {
                    earlyWarningNode.setResUser(resUser);
                    earlyWarningNode.setResUserPhone(resPhone);
                }
                earlyWarningNodeService.batchUpdate(earlyWarningNodes);
                //督查表
                t.setResUser(resUser);
                t.setResPhone(resPhone);
                //绩效表
                List<GradePoint> gradePoints = gradePointService.getGradeById(t.getSysId());
                for (GradePoint g : gradePoints) {
                    if ("业主单位责任人".equals(g.getCheckType())) {
                        g.setCheckObject(resUser);
                        g.setCheckObjectId(userId);
                    }
                }
                gradePointService.batchUpdate(gradePoints);
            }
        }
        supervisionTaskService.batchUpdate(tasks);
    }
}
